﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

public partial class spider_inner : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    protected string getHtmlFrom(string url)
    {
        HttpWebRequest hr = (HttpWebRequest)HttpWebRequest.Create(url);
        hr.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
        hr.Method = "Get";
        hr.KeepAlive = false;

        WebProxy wr = new WebProxy("172.16.0.20", 3128);
        NetworkCredential nc = new NetworkCredential("zhou-li", "@#$@for2009");
        wr.Credentials = nc;
        hr.Proxy = wr;
        HttpWebResponse hw = (HttpWebResponse)hr.GetResponse();
        Stream sm = hw.GetResponseStream();
        StreamReader sr = new StreamReader(sm, Encoding.Default);
        string str=sr.ReadToEnd();
        str=str.Replace("<!--", "").Replace("-->", "").Replace("<!","").Replace("\"","").Replace("\r","").Replace("\n","");
        //Regex rg = new Regex("-->");
        //str = rg.Split(str)[1];
        // href="http://www.wapurl.com/wapie.php?wapurl=http://hwo.cn" 239 target=_blank>好喔书城</a
        Regex rg = new Regex(@"href\=http\:\/\/www.wapurl.com\/wapie\.php\?wapurl\=[\w\.\-\/\:]+target=_blank\>.+\<\/a\>",RegexOptions.ECMAScript);
        //MatchCollection mcs = rg.Matches(str);
        //for (int i = 0;  i < mcs.Count; i++)
        //{
          //  Response.Write(mcs[i].Groups[0].Value + ":" + mcs[i].Groups[1].Value + "<br /)");
       // }
       
        Response.Write(rg.Match(str).Value.ToString());
        return str;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "load", "<script>$('#inner').val('" + getHtmlFrom("http://www.wapurl.com") + "');</script>");//dianping
       
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Regex rg = new Regex(@"href\=http\:\/\/www.wapurl.com\/wapie\.php\?wapurl\=[\w\.\-\/\:]+\s+\d+\s+target=_blank\>[^\<]+\<", RegexOptions.ECMAScript);
        Response.Write(rg.IsMatch(@"href=http://www.wapurl.com/wapie.php?wapurl=http://xxxxxw.sdfa.sfx 226 target=_blank>ssb<").ToString());
        
    }
}
